package util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtil {
    public static Connection getConnection(){
        Properties props = new Properties();
        InputStream in;
        try{
            in = DBUtil.class.getResourceAsStream("database.properties");
            try{
                props.load(in);
            }
            catch (IOException e1){
                e1.printStackTrace();
            }
        } catch (Exception e2){
            e2.printStackTrace();
        }
        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");
        String driver = props.getProperty("jdbc.driver");
        Connection con =null;
        try{
            Class.forName(driver);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        try{
            con = DriverManager.getConnection(url,username,password);
            System.out.println("Connection database success!");
        } catch (SQLException e){
            e.printStackTrace();
        }
        return con;
    }
        public static void close(Connection con){
        try{
            if(con!=null) con.close();
        } catch (SQLException e){
            e.printStackTrace();
        }
        }
    }
